/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.service.bmp.biz.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.page.Pager;
import com.xbdun.dao.bmp.dto.OrleDepartDto;
import com.xbdun.dao.bmp.entity.XbdunOrgUserDo;
import com.xbdun.dao.bmp.entity.XbdunOrleDepartDo;
import com.xbdun.dao.bmp.mapper.OrgUserMapper;
import com.xbdun.dao.bmp.mapper.OrleDepartMapper;
import com.xbdun.facade.bmp.request.system.OrleDepartPagerRequest;
import com.xbdun.facade.bmp.response.system.OrleDepartPagerResponse;
import com.xbdun.service.bmp.biz.OrleDepartBiz;


/**
 * 部门角色中间表
 * @ClassName XbdunOrleDepartServiceImpl
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Service
@Transactional
public class OrleDepartBizImpl implements OrleDepartBiz {
	@Autowired
	OrleDepartMapper orleDepartMapper;
	@Autowired
	OrgUserMapper orgUserMapper;
	Log log = LogFactory.getLog(OrleDepartBizImpl.class);

	/**
	 * 根据主键获取对象
	 * @return {@link XbdunOrleDepartDo}
	 */
	public XbdunOrleDepartDo getXbdunOrleDepartById(long id,long orgCode)throws RuntimeException{
		
		return orleDepartMapper.getXbdunOrleDepartById(id,orgCode);
	}

	/**
	 * 删除数据根据主键
	 * @param idx
	 */
	public int delXbdunOrleDepartById(long id,long orgCode,Timestamp version)throws RuntimeException{
		
		return orleDepartMapper.delXbdunOrleDepartById(id,orgCode,version);
	}
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param id 主键id
	 * @return Long
	 */
	public boolean existById(long id)throws RuntimeException{
		Assert.isTrue(id>0,"参数id必须大于零");
		Map<String,Object> param = new HashMap<String, Object>();
		
		//根据主键
		param.put("id",id);
		
		Long rlt = orleDepartMapper.existXbdunOrleDepart(param);

		param.clear();
		param = null;

		return (null==rlt||rlt.longValue()==0?false:true);
	}
	
	/**
	 * 更新对象属性
	 * @param XbdunOrleDepartDo	更新对象
	 * @param id
	 */
	public void updateById(XbdunOrleDepartDo xbdunOrleDepartDo,long id)throws RuntimeException{
		
		Assert.isTrue(null!=xbdunOrleDepartDo,"更新xbdunOrleDepartDo对象不能为空");
		
		Map<String,Object> param = new HashMap<String,Object>();
		//TODO 自定义更新对象属性 
		/**
		 param.put("orgCode", xbdunOrleDepartDo.getOrgCode());
		 param.put("roleId", xbdunOrleDepartDo.getRoleId());
		 param.put("code", xbdunOrleDepartDo.getCode());
		 param.put("createTime", xbdunOrleDepartDo.getCreateTime());
		 param.put("optionUser", xbdunOrleDepartDo.getOptionUser());
		 param.put("version", xbdunOrleDepartDo.getVersion());
		 param.put("blSpecial", xbdunOrleDepartDo.getBlSpecial());

		 * 属性对应的Map的键-值
		 */
		Map<String,Object> where = new HashMap<String,Object>();
		
		where.put("id", id);
		
		orleDepartMapper.updateDynXbdunOrleDepart(param, where);

		param.clear();
		where.clear();
		param = null;
		where = null;
	}
	
	/**
	 * 查询分页
	 * @param orgCode		当前机构Code
	 * @param departCode	当前机构下的部门Code
	 * @param orderBy		排序条件
	 * @param pageIndex   	当前索引页
	 * @param pageSize	每页显示条数
	 * @return
	 * @throws RuntimeException
	 */
	public Pager<OrleDepartPagerResponse> queryPager(long orgCode,OrleDepartPagerRequest request){
		XbdunOrgUserDo byUserId  = null;
		if(StringUtils.isNotBlank(request.getUserId())){
			byUserId = orgUserMapper.getByUserId(request.getUserId());
			if(null ==byUserId){
				throw BizException.DB_SELECTONE_IS_NULL.newInstance("操作的用户不存在");
			}
			if(byUserId.getOrgCode()!=orgCode){
				throw BizException.POWER_FAIL.newInstance("对不起该用户不属于您机构下，您没有权限操作");
			}
		}
		
		//分页对象
		Pager<OrleDepartPagerResponse> resultPager = request.newPager(OrleDepartPagerResponse.class);
		
		Long resultCnt = orleDepartMapper.queryPagerCount(orgCode, request.getDepartCode());
		if(null!=resultCnt&&resultCnt.longValue()==0){
			
			resultPager.setTotalCount(0);
			return resultPager;
		}
		//获取查询结果集List
		List<OrleDepartDto> list = orleDepartMapper.queryPager(orgCode,(null!=byUserId?byUserId.getUserCode():null),request.getDepartCode(),(resultPager.getPageIndex()-1)*request.getPageSize(),request.getPageSize());
		if(!CollectionUtils.isEmpty(list)){
			resultPager.setData(new ArrayList<OrleDepartPagerResponse>());
			OrleDepartPagerResponse infoRes = null;
			try{
				for(OrleDepartDto info : list){
					infoRes = new OrleDepartPagerResponse();
					
					infoRes.setCreateTime(info.getCreateTime());
					infoRes.setId(info.getId()+"");
					infoRes.setOrgCode(info.getOrgCode()+"");
					infoRes.setOptionUser(info.getOptionUser());
					infoRes.setRoleName(info.getRoleName());
					infoRes.setRoleShort(info.getRoleShort());
					infoRes.setBlUserRole(info.getBlUserRole());
					resultPager.getData().add(infoRes);
				}
			}catch(Exception e){
				log.error("企业信息查询组合list异常",e);
			}finally{
				list.clear();
				list = null;
			}
		}
		//设置查询条数
		resultPager.setTotalCount(resultCnt.longValue());
		resultPager.setPager();
		return resultPager;
	}

	public void addOrleDeparts(XbdunOrleDepartDo xbdunOrleDepartDo, List<Long> roleId) {
		
		orleDepartMapper.addOrleDeparts(xbdunOrleDepartDo, roleId);
	}
}